package org.lanqiao.lg.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Founder;
import org.lanqiao.bean.Product;
import org.lanqiao.lg.dao.impl.FounderDaoImpl;
import org.lanqiao.lg.dao.impl.ProductDaoImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;

@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {
	Gson gson = new Gson();
	ProductDaoImpl prodao = new ProductDaoImpl();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String key = getString("key");
		System.out.println(request.getSession().getId() + key + "/id=" + id + "/typeU=" + typeU);
		redata.setMsg("");
		if (!isNull("key")) {
			switch (key) {
			case "insert":
				insertPro();
				break;
			case "select":
				selectPro();
				break;
			default:
				redata.setCode(3);
				redata.setMsg("key参数异常");
				redata.setData("");
				break;
			}
		} else {
			redata.setCode(3);
			redata.setMsg("key参数为空");
			redata.setData("");
		}
		System.out.println(redata.toString());
		response.getWriter().write(gson.toJson(redata));
	}

//	查询公司产品
	private void selectPro() {
		Integer companyId = getInt("companyId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		List<Map<String, Object>> selectProductByCId = prodao.selectProductByCId(companyId);
		if (selectProductByCId != null) {
			System.out.println(1);
			redata.setCode(2);
			redata.setMsg("selectProductByCId 成功");
			redata.setData(selectProductByCId);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(selectProductByCId);
			return;
		}
	}

// 添加公司的产品
	private void insertPro() {
		Integer companyId = getInt("companyId");
		String name = getString("name");
		String com = getString("com");
		String introduction = getString("introduction");
		Integer imgId = getInt("imgId");
		System.out.println("msg=" + redata.getMsg());
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		if ("undefined".equals(name)) {
			return;
		}
		Integer i = prodao.insertProduct(new Product(name, com, imgId, introduction, companyId));
		if (i > 0) {
			System.out.println(1);
			redata.setCode(2);
			redata.setMsg("insertFounder 成功");
			redata.setData(i);
			return;
		} else {
			redata.setCode(3);
			redata.setMsg("服务器异常");
			redata.setData(i);
			return;
		}

	}
}
